/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.menu;

import mtt.appinfo.ApplicationInfoController;
import mtt.appinfo.events.ShowApplicationInfoViewEvent;
import mtt.appoptions.ApplicationOptionsController;
import mtt.appoptions.events.ShowApplicationOptionsViewEvent;
import mtt.calendar.weekview.WeekController;
import mtt.calendar.weekview.events.ShowWeekViewEvent;
import mtt.intro.IntroController;
import mtt.intro.events.ShowIntroViewEvent;
import mtt.marks.MarksController;
import mtt.marks.events.ShowMarksViewEvent;
import mtt.menu.events.ShowApplicationInfo;
import mtt.menu.events.ShowApplicationOptionEvent;
import mtt.menu.events.ShowCalendarEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.menu.events.ShowMarksEvent;
import mtt.menu.mainmenu.MainMenuController;
import mtt.menu.mainmenu.events.ShowMainMenuViewEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;

/**
 * 
 * @author jens.meiss
 */
public class MenuController extends AController {

	private MenuModel menuModel;
	
	public MenuController() {
		
		setMenuModel(new MenuModel());
		
	}

	private MenuModel getMenuModel() {
		return menuModel;
	}

	public void receiveEvent(IEvent event) {

		if (event instanceof ShowCalendarEvent) {
			showCalendar();
		} else if (event instanceof ShowMainMenuEvent) {
			showMainMenu();
		} else if (event instanceof ShowMarksEvent) {
			showMarks();
		} else if (event instanceof ShowApplicationInfo) {
			showApplicationInfo();
		} else if (event instanceof ShowApplicationOptionEvent) {
			showApplicationOptions();
		}

	}

	private void setMenuModel(MenuModel menuModel) {
		this.menuModel = menuModel;
	}

	private void showApplicationInfo() {
		addChildController(new ApplicationInfoController());
		fireEventDown(new ShowApplicationInfoViewEvent());
	}

	private void showApplicationOptions() {
		
		addChildController(new ApplicationOptionsController());
		fireEventDown(new ShowApplicationOptionsViewEvent());
	}

	private void showCalendar() {
		addChildController(new WeekController());
		fireEventDown(new ShowWeekViewEvent());
	}

	private void showMainMenu() {

		if (getMenuModel().getControllerMainMenu() == null) {
			
			MainMenuController controllerMainMenu = new MainMenuController();
			getMenuModel().setControllerMainMenu(controllerMainMenu);
			addChildController(controllerMainMenu);
			
			
		}

		fireEventDown(new ShowMainMenuViewEvent());

	}

	private void showMarks() {

		addChildController(new MarksController());
		fireEventDown(new ShowMarksViewEvent());

	}

	public void terminate() {
	}

	public void init() {
		
		register(ShowApplicationInfo.class);
		register(ShowMarksEvent.class);
		register(ShowApplicationOptionEvent.class);
		register(ShowCalendarEvent.class);
		register(ShowMainMenuEvent.class);
		
		addChildController(new IntroController());
		
		fireEventDown(new ShowIntroViewEvent());
	}

}
